package com.yt.setclaim.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.setclaim.server.mapper.FinancialPayMapper;
import com.yt.setclaim.server.mapper.ClaimCalcMapper;
import com.yt.setclaim.models.entity.FinancialPay;
import com.yt.setclaim.models.entity.ClaimCalc;
import com.yt.setclaim.models.service.FinancialPayService;
import com.yt.setclaim.models.vo.ClaimCalcVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FinancialPayServiceImpl extends ServiceImpl<FinancialPayMapper, FinancialPay> implements FinancialPayService {
    
    private final ClaimCalcMapper claimCalcMapper;
    
    public FinancialPayServiceImpl(ClaimCalcMapper claimCalcMapper) {
        this.claimCalcMapper = claimCalcMapper;
    }
    
    @Override
    public FinancialPay getByCalcId(Integer calcId) {
        return this.getOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<FinancialPay>()
                .eq("calc_id", calcId)
                .eq("is_delete", 0));
    }
    
    @Override
    public List<ClaimCalcVO> getPendingClaims() {
        // 获取所有已核算但未支付的理赔核算记录
        // 1. 首先获取所有理赔核算记录
        QueryWrapper<ClaimCalc> calcQueryWrapper = new QueryWrapper<>();
        calcQueryWrapper.eq("is_delete", 0); // 未删除
        List<ClaimCalc> claimCalcs = claimCalcMapper.selectList(calcQueryWrapper);
        
        // 2. 过滤掉已经进行过支付的记录
        List<ClaimCalcVO> pendingClaims = new ArrayList<>();
        for (ClaimCalc calc : claimCalcs) {
            // 检查是否已经支付过
            FinancialPay existingPay = this.getOne(new QueryWrapper<FinancialPay>()
                    .eq("calc_id", calc.getCalcId())
                    .eq("is_delete", 0));
            
            // 如果还没有支付，则添加到待支付列表中
            if (existingPay == null) {
                ClaimCalcVO vo = new ClaimCalcVO();
                BeanUtils.copyProperties(calc, vo);
                // ID转换为字符串类型
                vo.setCalcId(String.valueOf(calc.getCalcId()));
                vo.setReviewId(String.valueOf(calc.getReviewId()));
                vo.setAdminId(String.valueOf(calc.getAdminId()));
                // 业务字段转换为字符串类型
                if (calc.getBaseAmount() != null) {
                    vo.setBaseAmount(String.valueOf(calc.getBaseAmount()));
                }
                if (calc.getTotalDeduct() != null) {
                    vo.setTotalDeduct(String.valueOf(calc.getTotalDeduct()));
                }
                if (calc.getPayRatio() != null) {
                    vo.setPayRatio(String.valueOf(calc.getPayRatio()));
                }
                if (calc.getPayAmount() != null) {
                    vo.setPayAmount(String.valueOf(calc.getPayAmount()));
                }
                pendingClaims.add(vo);
            }
        }
        
        return pendingClaims;
    }
}